import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
from sklearn import metrics
from data_preprocessing import data_process, distinct_, get_custom_stopwords, chinese_word_cut


def run_bayes():
    # 读取文件 
    positive = pd.read_csv(r'G:\毕业设计\情感分析\corpus\positive.txt', header=None, names=['comment', 'label'])
    negtive = pd.read_csv(r'G:\毕业设计\情感分析\corpus\negtive.txt', header=None, names=['comment', 'label'])

    # 合并两个dataframe
    data = pd.concat([positive[['comment', 'label']], negtive[['comment', 'label']]])

    # 数据预处理
    data['comment'] = data['comment'].apply(lambda x: distinct_(x))
    data['comment'] = data['comment'].apply(lambda x: data_process(x))
    data.dropna(axis=0, subset=['comment'], inplace=True)

    # 获取哈工大停用词
    stopwords = get_custom_stopwords(r'G:\毕业设计\myspider\stopwords-master\hit_stopwords.txt')

    # 使用jieba分词
    data['cutted_comment'] = data['comment'].apply(chinese_word_cut)

    # 生成X,y
    X = data[['cutted_comment']]
    y = data['label']

    # 分成训练集与测试集
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=666)

    # 提取特征,过滤不必要的词语,转换词频矩阵
    vect = CountVectorizer(max_df=0.8,
                           min_df=3,
                           token_pattern=u'(?u)\\b[^\\d\\W]\\w+\\b',
                           stop_words=frozenset(stopwords))

    # 统计出现次数
    term_matrix = pd.DataFrame(vect.fit_transform(X_train.cutted_comment).toarray(), columns=vect.get_feature_names())

    # 建立朴素贝叶斯分类器
    nb = MultinomialNB()

    # 建立一个导管,将分类器和向量化函数联系一起
    pipe = make_pipeline(vect, nb)

    # 训练模型
    pipe.fit(X_train['cutted_comment'], y_train)

    # 预测结果
    y_pred = pipe.predict(X_test['cutted_comment'])

    # 查看分数
    print("解释分类模型的f1_score: %0.3f " % metrics.f1_score(y_test, y_pred))

    # 查看混淆矩阵
    print(metrics.confusion_matrix(y_test, y_pred))


if __name__ == '__main__':
    run_bayes()
